package com.cyxu.moviedigest.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.cyxu.moviedigest.R;
import com.cyxu.moviedigest.data.AppConstant;
import com.cyxu.moviedigest.data.MovieInfo;

import org.json.JSONArray;
import org.json.JSONException;


public class MovieDetailActivity extends ActionBarActivity {

    private MovieInfo movie;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_detail);

        Intent intent = getIntent();
        movie = (MovieInfo)intent.getSerializableExtra(AppConstant.EXTRA_MOVIE);

        // poster of movie
        RequestQueue queue = Volley.newRequestQueue(this);
        ImageRequest imageRequest = new ImageRequest(movie.getImage(), new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                ImageView iv_poster = (ImageView) findViewById(R.id.movie_detail_pic);
                iv_poster.setScaleType(ImageView.ScaleType.CENTER_CROP);
                iv_poster.setImageBitmap(bitmap);
            }
        }, 0, 0, ImageView.ScaleType.CENTER_CROP, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        queue.add(imageRequest);

        // details of movie
        TextView tv_title = (TextView) findViewById(R.id.movie_detail_name);
        tv_title.setText(movie.getTitle());

        TextView tv_desc = (TextView) findViewById(R.id.movie_detail_description);
        tv_desc.setText(movie.getDescription());

        String directors = "Directors: ";
        try {
            JSONArray jDirectors = new JSONArray(movie.getDirectors());
            for (int i = 0; i < jDirectors.length() - 1; ++i) {
                directors += jDirectors.getString(i) + ", ";
            }
            directors += jDirectors.getString(jDirectors.length() - 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        TextView tv_direct = (TextView) findViewById(R.id.movie_detail_director);
        tv_direct.setText(directors);

        String stars = "Stars: ";
        try {
            JSONArray jStars = new JSONArray(movie.getCasts());
            for (int i = 0; i < jStars.length() - 1; ++i) {
                stars += jStars.getString(i) + ", ";
            }
            stars += jStars.getString(jStars.length() - 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        TextView tv_casts = (TextView) findViewById(R.id.movie_detail_star);
        tv_casts.setText(stars);

        TextView tv_year = (TextView) findViewById(R.id.movie_detail_year);
        tv_year.setText(Integer.toString(movie.getYear()));

        TextView tv_duration = (TextView) findViewById(R.id.movie_detail_duration);
        tv_duration.setText(movie.getDuration() + "min");

        String genres = "";
        try {
            JSONArray jGenres = new JSONArray(movie.getGenres());
            for (int i = 0; i < jGenres.length() - 1; ++i) {
                genres += jGenres.getString(i) + ", ";
            }
            genres += jGenres.getString(jGenres.length() - 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        TextView tv_genres = (TextView) findViewById(R.id.movie_detail_genres);
        tv_genres.setText(genres);

        TextView tv_rate = (TextView) findViewById(R.id.movie_detail_rate);
        tv_rate.setText(movie.getRate() + "/10");
    }

}
